package com.bjbn.controller;

import com.bjbn.service.HistoryDataCleanupService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 历史数据清理控制器
 */
@Slf4j
@RestController
@RequestMapping("/cleanup")
@Tag(name = "历史数据清理", description = "历史数据清理相关接口")
public class HistoryDataCleanupController {

    @Autowired
    private HistoryDataCleanupService historyDataCleanupService;

    /**
     * 手动清理温湿度传感器历史数据
     * 
     * @return 清理结果
     */
    @Operation(summary = "清理温湿度传感器历史数据", description = "手动触发温湿度传感器历史数据清理，删除超过保留期限的数据")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "清理成功"),
            @ApiResponse(responseCode = "500", description = "清理失败")
    })
    @PostMapping("/ths")
    public Map<String, Object> cleanupThsHistoryData() {
        Map<String, Object> result = new HashMap<>();
        
        try {
            log.info("手动触发温湿度传感器历史数据清理");
            
            int deletedCount = historyDataCleanupService.cleanupThsHistoryData();
            
            result.put("success", true);
            result.put("message", "温湿度传感器历史数据清理完成");
            result.put("deletedCount", deletedCount);
            
            log.info("手动温湿度传感器历史数据清理完成，删除{}条记录", deletedCount);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "温湿度传感器历史数据清理失败：" + e.getMessage());
            result.put("deletedCount", 0);
            
            log.error("手动温湿度传感器历史数据清理异常", e);
        }
        
        return result;
    }

    /**
     * 手动清理照度传感器历史数据
     * 
     * @return 清理结果
     */
    @Operation(summary = "清理照度传感器历史数据", description = "手动触发照度传感器历史数据清理，删除超过保留期限的数据")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "清理成功"),
            @ApiResponse(responseCode = "500", description = "清理失败")
    })
    @PostMapping("/lx")
    public Map<String, Object> cleanupLxHistoryData() {
        Map<String, Object> result = new HashMap<>();
        
        try {
            log.info("手动触发照度传感器历史数据清理");
            
            int deletedCount = historyDataCleanupService.cleanupLxHistoryData();
            
            result.put("success", true);
            result.put("message", "照度传感器历史数据清理完成");
            result.put("deletedCount", deletedCount);
            
            log.info("手动照度传感器历史数据清理完成，删除{}条记录", deletedCount);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "照度传感器历史数据清理失败：" + e.getMessage());
            result.put("deletedCount", 0);
            
            log.error("手动照度传感器历史数据清理异常", e);
        }
        
        return result;
    }

    /**
     * 手动清理所有传感器历史数据
     * 
     * @return 清理结果
     */
    @Operation(summary = "清理所有传感器历史数据", description = "手动触发所有传感器历史数据清理，包括温湿度和照度传感器")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "清理成功"),
            @ApiResponse(responseCode = "500", description = "清理失败")
    })
    @PostMapping("/all")
    public Map<String, Object> cleanupAllHistoryData() {
        Map<String, Object> result = new HashMap<>();
        
        try {
            log.info("手动触发所有传感器历史数据清理");
            
            String cleanupResult = historyDataCleanupService.cleanupAllHistoryData();
            
            result.put("success", true);
            result.put("message", "所有传感器历史数据清理完成");
            result.put("result", cleanupResult);
            
            log.info("手动所有传感器历史数据清理完成: {}", cleanupResult);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "所有传感器历史数据清理失败：" + e.getMessage());
            result.put("result", null);
            
            log.error("手动所有传感器历史数据清理异常", e);
        }
        
        return result;
    }

    /**
     * 获取历史数据清理策略信息
     * 
     * @return 清理策略信息
     */
    @Operation(summary = "获取清理策略信息", description = "获取历史数据清理的策略配置和规则信息")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "获取成功"),
            @ApiResponse(responseCode = "500", description = "获取失败")
    })
    @GetMapping("/info")
    public Map<String, Object> getCleanupInfo() {
        Map<String, Object> result = new HashMap<>();
        
        try {
            Map<String, Object> info = new HashMap<>();
            info.put("strategy", "保留一个月的历史数据");
            info.put("dailyCleanup", "每天凌晨2点自动清理");
            info.put("weeklyThsCleanup", "每周日凌晨3点清理温湿度传感器数据");
            info.put("weeklyLxCleanup", "每周日凌晨4点清理照度传感器数据");
            info.put("manualCleanup", "支持手动触发清理");
            
            result.put("success", true);
            result.put("message", "获取清理策略信息成功");
            result.put("data", info);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "获取清理策略信息失败：" + e.getMessage());
            result.put("data", null);
        }
        
        return result;
    }
} 